package com.caocaog.market.finance.pay.alipay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.caocaog.market.finance.pay.vo.BizContent;
import com.caocaog.market.finance.pay.vo.FinanceVo;
import com.caocaog.market.finance.pay.vo.PayeeInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;


/**
 * @Description: AliPay
 * @author: Yuki Guo
 * @date: 2021年11月1日 10:12
 */

@Slf4j
@Configuration
public class FinancePay {

    @Autowired
    private FinancePayBean aliPayBean;

    /**
     * 单笔转账到支付宝账户
     * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.54Ty29&treeId=193&articleId=106236&docType=1
     */
    public AlipayFundTransUniTransferResponse transfer(FinanceVo transVo) {
        log.info("transfer in -----params transVo :"+transVo.toString());
        System.out.println("transfer in -----params aliPayBean :"+aliPayBean.toString());
        try {
            CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
            certAlipayRequest.setServerUrl(aliPayBean.getServerUrl());
            certAlipayRequest.setAppId(aliPayBean.getAppId());
            certAlipayRequest.setPrivateKey(aliPayBean.getPrivateKey());
            certAlipayRequest.setFormat("JSON");
            certAlipayRequest.setCharset("GBK");
            certAlipayRequest.setSignType("RSA2");
            certAlipayRequest.setCertPath(aliPayBean.getAppCertPath());
            certAlipayRequest.setAlipayPublicCertPath(aliPayBean.getAliPayCertPath());
            certAlipayRequest.setRootCertPath(aliPayBean.getAliPayRootCertPath());
            AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
            AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
            BizContent vo = new BizContent();
            PayeeInfo payeeInfo = new PayeeInfo();
            payeeInfo.setIdentity(transVo.getZfbAccount());
            payeeInfo.setIdentity_type("ALIPAY_LOGON_ID");// 当前为固定参数 ALIPAY_LOGON_ID：支付宝登录号，支持邮箱和手机号格式。
            payeeInfo.setName(transVo.getZfbName());
            vo.setPayee_info(payeeInfo);
            vo.setOut_biz_no(transVo.getOutBizNo());
            vo.setOrder_title("曹操来了-转账-订单号-"+transVo.getOutBizNo());
            vo.setRemark(transVo.getRemark());
            vo.setTrans_amount(transVo.getAmount());
            vo.setBusiness_params("{\"payer_show_name\":\"曹操来了-转账\"}");
            System.out.println(JSON.toJSONString(vo));
            request.setBizContent(JSON.toJSONString(vo));
            AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);

            log.info("response : "+ JSONArray.toJSONString(response));
            log.info("response : "+response.isSuccess());
            log.info("response : "+response.getCode());
            log.info("response : "+response.getMsg());
            return response;
        } catch (AlipayApiException e) {
            log.error("transfer system error:{}", e.getMessage());
           throw new JeecgBootException("转账操作失败~");
        }

    }

}
